import 'package:flutter/material.dart';
import 'package:flutter_fun_app/model/videoModel.dart';
import 'package:flutter_fun_app/model/videoListModel.dart';
import 'package:flutter_common/net/http.dart';
import 'package:flutter_common/widget/funAppBar.dart';
import 'package:flutter_common/widget/loadingAnim.dart';
import 'package:flutter_fun_app/page/videoDetailPage.dart';

class VideoListPage extends StatefulWidget {
  ItemList itemList;

  VideoListPage(this.itemList);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return VideoListState(itemList);
  }
}

class VideoListState extends State<VideoListPage> {
  ItemList itemList;

  VideoListState(this.itemList);

  List<VideoResult> videoList = List();
  bool refresh = true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getVideoListData();
  }

  Future<void> getVideoListData() async {
    var data = {'id': itemList.data.id};
    HttpUtil.get('https://api.apiopen.top/videoCategoryDetails', data: data,
        success: (data) {
      VideoDetailModel detailModel = VideoDetailModel.fromJson(data);
      if (detailModel.code == 200) {
        setState(() {
          refresh = false;
          videoList = detailModel.result;
        });
      }
    }, then: () {});
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: FunAppBar(
          title: itemList.data.description,
        ),
        body: refresh
            ? LoadingAnim.getProgressAnim()
            : ListView.builder(
                itemCount: videoList.length,
                itemBuilder: (BuildContext context, int index) {
                  return itemBuilder(index);
                }));
  }

  Widget itemBuilder(int index) {
    VideoResult result = videoList[index];
    return Card(
      color: Colors.white,
      child: Padding(
        padding: EdgeInsets.all(5),
        child: Column(
          children: <Widget>[
            Stack(
              children: <Widget>[
                Image.network(
                  result.data.content.data.cover.detail,
                  fit: BoxFit.fill,
                  width: double.infinity,
                  height: 210,
                ),
                Container(
                  height: 210,
                  alignment: Alignment.center,
                  child: IconButton(
                      icon: Icon(
                        Icons.play_circle_outline,
                        size: 60,
                      ),
                      color: Colors.white,
                      onPressed: () {
                        Navigator.push(
                            context,
                            new MaterialPageRoute(
                                builder: (context) => VideoDetailPage(result)));
                      }),
                )
              ],
            ),
            Padding(padding: EdgeInsets.only(top: 5)),
            Container(
              alignment: AlignmentDirectional.topStart,
              child: Text(result.data.content.data.description,
                  maxLines: 3,
                  overflow: TextOverflow.ellipsis),
            )
          ],
        ),
      ),
    );
  }
}
